<?php


namespace app\home\controller;


use app\BaseController;
use app\Request;
use think\facade\Db;

class BookList extends BaseController
{
    protected $middleware = [\app\middleware\Check2::class];

    public function index()
    {
        return view();
    }

    //    分页展示
    public function article(Request $request){

//        查询条件
        $where = [];
        if (!empty($request->param('name'))) {
            $where[] = ['id|bname|author', 'like', "%{$request->param('name')}%"];
        }
        //获取每页显示的条数
        $limit= $request->param('limit');
        //获取当前页数
        $page= $request->param('page');
        $list = Db::name('book')
            ->where($where)
            ->where('count','<>',0)
            ->where('status','=',1)
            ->alias('b')
            ->join('storage c','b.id = c.id')
            ->paginate($limit,false,['page'=>$page])
            ->toArray();
        return json(['code'=>0,'msg'=>'','count'=>$list['total'],'data'=>$list['data']]);

    }

//    借阅
   public function borrow(){

       $data = request()->param();
       $book = \app\model\Book::find($data['id']);
       $book->count  =  $book['count']-1;
       $book->lend_number    = $book['lend_number']+1;
       $ret1 = $book->save();
       $data1 = [
           'book_id' => $data['id'],
           'user_id' =>  session('id'),
           'lending_date'    => $data['lending_data'],
           'should_return_date'     => $data['should_return_data'],
       ];

       $Db = Db::name('book_lending');

       $ret2 = $Db->insert($data1);

       if($ret1&&$ret2){
           return  json(["state"=>200]);
       }
       else{
           return json(["state"=>204,"message"=>'修改信息失败']);
       }
   }
}